package facade;

import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import other.ObservableJTabbedPane;

import customGUI.MainMenuContent;

import db.Account;

import builder.FormBuilder;
import builder.FormDirector;
import builder.AccountFormBuilderPackage.AccountFormBuilder;
import builder.AccountFormBuilderPackage.AdminAccountDecorator;
import builder.AccountFormBuilderPackage.LoginDecorator;
import builder.AccountFormBuilderPackage.OtherInfoDecorator;
import builder.AccountFormBuilderPackage.UneditableAccountFormDecorator;
import builder.AccountFormBuilderPackage.UpdateAccountDecorator;
import builder.AssetFormBuilderPackage.CoreAssetFormBuilder;
import builder.AssetFormBuilderPackage.CreateDecorator;
import builder.AssetFormBuilderPackage.RetrieveDecorator;
import builder.AssetFormBuilderPackage.UpdateDecorator;
import builder.AssetFormBuilderPackage.ViewAssetFormBuilder;
import builder.LogFormBuilderPackage.LogFormBuilder;

import form.Form;
import form.AccountForm.AccountForm;
import form.AssetForm.AssetForm;

public class FormCreator {
	
	public static final String ASSET_MANAGER = "MAN";
	public static final String ADMIN = "ADM";
	public static final String MEMBER = "MEM";
	public static final String NEW_MEMBER = "NEW";
	
	public static ArrayList<MainMenuContent> createMainMenuContentList(Account account){
		ArrayList<MainMenuContent> content = new ArrayList<MainMenuContent>();
		
		MainMenuContent contentAssets = createContentAssets(account);
		if(contentAssets != null)
			content.add(contentAssets);
		
		MainMenuContent contentAccounts = createContentAccounts(account);
		if(contentAccounts!=null)
			content.add(contentAccounts);
		
		MainMenuContent contentLogs = createContentLogs(account);
		if(contentLogs!=null)
			content.add(contentLogs);
	
		return content;
	}
	
	private static MainMenuContent createContentAssets(Account account){
		String userType = account.getAcctTypeCode();
				
		//create assets
		AssetForm[] assetForms = createAssetForms(userType);
		
		if(assetForms.length == 0)
			return null;
		
		JTabbedPane tabbedPane = new JTabbedPane();
		for(int i=0;i<assetForms.length;i++)
			tabbedPane.add(assetForms[i].getFormTitle(), assetForms[i]);
		JPanel assetPanel = new JPanel();
		assetPanel.add(tabbedPane);
		
		
		ObservableJTabbedPane observableTabbedPane = new ObservableJTabbedPane(tabbedPane);
		for(int i=0;i<assetForms.length;i++)
		{
			observableTabbedPane.addObserver(assetForms[i]);
			//System.out.println(assetForms[i].getFormTitle()+" is now observing.");
		}
		
		//System.out.println("I have "+observableTabbedPane.countObservers()+"observers.");
		
		return new MainMenuContent("Assets", assetPanel);	
	}
	
	private static MainMenuContent createContentAccounts(Account account){
		String userType = account.getAcctTypeCode();
		
		AccountForm[] accountForm = createAccountForms(userType);
		
		if(accountForm.length == 0)
			return null;
		
		JTabbedPane tabbedPane = new JTabbedPane();
		for(int i = 0; i < accountForm.length; i++)
		{
			tabbedPane.add(accountForm[i].getFormTitle(), accountForm[i]);
			accountForm[i].populate(account);
		}
		JPanel accountPanel = new JPanel();
		accountPanel.add(tabbedPane);
		
		ObservableJTabbedPane observableTabbedPane = new ObservableJTabbedPane(tabbedPane);
		for(int i = 0; i < accountForm.length; i++)
		{
			observableTabbedPane.addObserver(accountForm[i]);
			
		}
	
		return new MainMenuContent("Accounts", accountPanel);	
	
	}
	
	private static MainMenuContent createContentLogs(Account account){
		String userType = account.getAcctTypeCode();
		
		if(userType.equals(ASSET_MANAGER)){
			FormBuilder logFormBuilder = new LogFormBuilder();
			FormDirector formDirector = new FormDirector(logFormBuilder);
			formDirector.construct();
			return new MainMenuContent("Logs", formDirector.getForm());
		}
		else if(userType.equals(MEMBER) || userType.equals(ADMIN) || userType.equals(NEW_MEMBER)){
			return null;
		}
		
		
		return null;
	}
		
	public static AssetForm[] createAssetForms(String userType){
		
		AssetForm[] emptyForms = {};
		if(userType.equals(ADMIN)||userType.equals(NEW_MEMBER))
		{
			return emptyForms;
		}
		else if(userType.equals(ASSET_MANAGER))
		{
			AssetForm[] managerForms = {createAddAssetForm(), createEditAssetForm(), createViewAssetForm()};
			return managerForms;
		}
		else if(userType.equals(MEMBER))
		{
			AssetForm[] memberForms = {createViewAssetForm()};
			return memberForms;
		}

		
		return emptyForms;
	}
	
	public static AccountForm[] createAccountForms(String userType){
		
		AccountForm[] memberForms = {createEditAccountForm()};
		if (userType.equals(ADMIN))
		{
			AccountForm[] adminForms = {createEditAccountForm(), createManageAccountForm()};
			return adminForms;
		}
		else if (userType.equals(ASSET_MANAGER) || userType.equals(MEMBER) || userType.equals(NEW_MEMBER))
		{
			return memberForms;
		}
			return memberForms;
	}
	
	public static AccountForm createLogInForm(){
		FormBuilder loginbuilder = new AccountFormBuilder();
		loginbuilder = new LoginDecorator(loginbuilder);
		FormDirector logindirector = new FormDirector(loginbuilder);
		logindirector.construct();
		return (AccountForm)logindirector.getForm();
	}
	
	public static AssetForm createAddAssetForm(){
		FormBuilder addBuilder = new CoreAssetFormBuilder();
    	addBuilder = new CreateDecorator(addBuilder);
    	
    	FormDirector addDirector = new FormDirector(addBuilder);
    	addDirector.construct();
    	addDirector.getForm().setFormTitle("Add Asset");
    	
    	return (AssetForm)addDirector.getForm();
	}
	
	public static AssetForm createViewAssetForm(){
		/*
		FormBuilder builder = new CoreAssetFormBuilder();
    	builder = new RetrieveDecorator(builder);
    	builder = new UneditableAssetFormDecorator(builder);
    	*/
		
		FormBuilder builder = new ViewAssetFormBuilder();
		builder = new RetrieveDecorator(builder);
		
    	FormDirector director = new FormDirector(builder);
    	director.construct();
    	director.getForm().setFormTitle("View Assets");
    	
    	return (AssetForm)director.getForm();
	}
	
	public static AssetForm createEditAssetForm(){
		FormBuilder editBuilder = new CoreAssetFormBuilder();
		editBuilder = new RetrieveDecorator(editBuilder);
    	editBuilder = new UpdateDecorator(editBuilder);
    	
    	FormDirector editDirector = new FormDirector(editBuilder);
    	editDirector.construct();
    	editDirector.getForm().setFormTitle("Edit Assets");
		
    	return (AssetForm)editDirector.getForm();
	}
	
	public static AccountForm createEditAccountForm(){
		FormBuilder editAccountBuilder = new AccountFormBuilder();
		editAccountBuilder = new OtherInfoDecorator(editAccountBuilder);
		editAccountBuilder = new UpdateAccountDecorator(editAccountBuilder);
		FormDirector logindirector = new FormDirector(editAccountBuilder);
		logindirector.construct();
		logindirector.getForm().setFormTitle("Edit Account");
		return (AccountForm)logindirector.getForm();
	}
	
	public static AccountForm createManageAccountForm(){
		FormBuilder manageAccountBuilder = new AccountFormBuilder();
		manageAccountBuilder = new OtherInfoDecorator(manageAccountBuilder);
		manageAccountBuilder = new AdminAccountDecorator(manageAccountBuilder);
		manageAccountBuilder = new UneditableAccountFormDecorator(manageAccountBuilder);
		FormDirector logindirector = new FormDirector(manageAccountBuilder);
		logindirector.construct();
		logindirector.getForm().setFormTitle("Manage Account");
		return (AccountForm)logindirector.getForm();
		
	}
	
}
